#include "Client_fltk.h"

using namespace std;

void error(const char *msg)
{
    perror(msg);
    exit(0);
}

void easy_cb(Fl_Widget* o, void*)
{
    //send a message to the server to chage the difficulty
    string temp = "EASY";
    send_message(temp);

}

void medium_cb(Fl_Widget* o, void*)
{
    string temp = "MEDIUM";
    send_message(temp);
}

void hard_cb(Fl_Widget* o, void*)
{
    string temp = "HARD";
    send_message(temp);
}

void ai_connect_cb(Fl_Widget* o, void*)
{
    Fl_Button* b=(Fl_Button*)o;
    Fl_Input* iw_server  = (Fl_Input*) b -> parent() -> child(2);
    Fl_Input* iw_port    = (Fl_Input*) b -> parent() -> child(3);

    next_server   = iw_server->value();
    next_port     = iw_port->value();

    connect_win->hide();

    string temp = "AI-AI "+next_server+" "+next_port;
    send_message(temp);
}

void ai_cb(Fl_Widget* o, void*)
{
    ai_human = false;

    connect_win->begin();      
    Fl_Button*  connect = new Fl_Button( 10, 150, 70, 30, "Connect"); //child 0   : 1st widget
    //Fl_Button* close = new Fl_Button(100, 150, 70, 30, "&Quit"); //child 1    : 2nd widget
    Fl_Input*       server= new Fl_Input(50, 50, 140, 30, "Server");//child 2 : 3rd widget
    Fl_Input*       port  = new Fl_Input(50, 100, 140, 30, "Port"); //child 3   : 4th widget 
    //Fl_Output*    out = new Fl_Output(50, 100, 140, 30, "Out"); 
    connect_win->end();
    connect->callback(ai_connect_cb);
    //close->callback( quit_cb );
    connect_win->show();
}

void human_cb(Fl_Widget* o, void*)
{
    ai_human = true;
    string temp = "HUMAN_AI";
    send_message(temp);
}

void check_ai(char* buff)
{
    stringstream ss;
    string temp;
    ss << buff;
    ss >> temp;

    if(temp == "AI-AI")
        ai_human = false;
    else
        ai_human = true;
}
void quit_cb(Fl_Widget *, void *) {
    //write Close to Server
    string temp = "Close";
    strcpy ( buffer, temp.c_str() );
    write(sockfd,buffer,strlen(buffer));
    close(sockfd);
    exit(0);
}

void connect_but_cb( Fl_Widget* o , void*) {

    Fl_Button* b=(Fl_Button*)o;
    Fl_Input* iw_server  = (Fl_Input*) b -> parent() -> child(2);
    Fl_Input* iw_port    = (Fl_Input*) b -> parent() -> child(3);

    sr   = iw_server->value();
    pt     = iw_port->value();

    connect_win->hide();

    
    int portno;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    portno = atoi(pt.c_str());
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
    server = gethostbyname(sr.c_str());
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR connecting");
   
    string tmp_string = "DISPLAY";
    send_message(tmp_string);
    grid_pointer->redraw();
    grid_pointer->connected = true;
    
}

void connect_cb(Fl_Widget *w, void*)
{
    connect_win->begin();      
    Fl_Button*  connect = new Fl_Button( 10, 150, 70, 30, "Connect"); //child 0   : 1st widget
    Fl_Button* close = new Fl_Button(100, 150, 70, 30, "&Quit"); //child 1    : 2nd widget
    Fl_Input*       server= new Fl_Input(50, 50, 140, 30, "Server");//child 2 : 3rd widget
    Fl_Input*       port  = new Fl_Input(50, 100, 140, 30, "Port"); //child 3   : 4th widget 
    //Fl_Output*    out = new Fl_Output(50, 100, 140, 30, "Out"); 
    connect_win->end();
    connect->callback(  connect_but_cb );
    close->callback( quit_cb );
    connect_win->show();
}

int main(int argc, char *argv[])
{

    win->begin();
    Fl_Menu_Bar *menu = new Fl_Menu_Bar(0,0,800,25);
    Grid* grid = new Grid(0,16,win->w(), win->h()-26);
    grid_pointer = grid;
    menu->add("File/Close",   FL_CTRL+'q', quit_cb);
    menu->add("Server/Connect", FL_CTRL+'c', connect_cb);
    menu->add("Difficulty/Easy", FL_CTRL+'e', easy_cb);
    menu->add("Difficulty/Medium", FL_CTRL+'m', medium_cb);
    menu->add("Difficulty/Hard", FL_CTRL+'h', hard_cb);
    menu->add("Play_mode/AI-AI", FL_CTRL+'a', ai_cb);
    menu->add("Play_mode/HUMAN-AI", FL_CTRL+'h', human_cb);
    win->end();
    win->show();
    return(Fl::run());
}
